<template>
	<view class="orders">
		<view style="position: fixed;left: 0;top: 0;background-color: #fff;z-index: 2;">
			<cc-newsTabs :tabArr="tabArr" @tabChange="tabChange"></cc-newsTabs>
		</view>
		<view class="list">
			<view class="li" v-for="item in list" :key="item.id">
				<view class="code">
					{{item.order_no}}
					<view class="status">{{item.status_text}}</view>
				</view>
				<view class="content">
					<image class="avatar" :src="item.store.image"></image>
					<view class="info">
						<view class="title">{{item.store.name}}</view>
						<view class="from">平台活动: {{item.activity.platform=='meituan'?'美团':'饿了么'}}</view>
						<view class="" v-if="item.status_text=='待上传'">
							<view class="jointime">返现门槛: {{item.vip_pay_fee}}</view>
							<view class="endtime">返现金额: {{item.vip_cash_back}}</view>
						</view>
						<view class="" v-else>
							<view class="jointime">参与时间:{{item.activity.showdate.split(' - ')[0]||''}}</view>
							<view class="endtime">失效时间: {{item.activity.showdate.split(' - ')[1]||''}}</view>
						</view>

					</view>

				</view>
				<view class="foot" v-if="item.status_text=='待上传'">
					<view class="comment">{{item.activity.rule}}</view>
					<vuew class="btns">
						<view class="btn-1" @click="cancelOrder(item)">取消报名</view>
						<view class="btn-2" @click="goNav(item)">上传凭证</view>
						<view class="btn-3" @click="storeTap(item)">进店下单</view>
					</vuew>
				</view>
				<view class="foot" v-if="item.status_text=='驳回'">
					<vuew class="btns">
						<view class="btn-2" @click="goNav(item)">上传凭证</view>
					</vuew>
				</view>
				<view class="foot" v-if="item.status_text=='未通过'">
					<vuew class="btns">
						<view class="btn-2" @click="goNav(item)">重新上传凭证</view>
					</vuew>
				</view>
			</view>
		</view>
		<view class="empty" v-if="empty">
			<image src="https://video.file.zhuochengyun.com/bwc/uploads/20240207/20240207224232736026.png"></image>
			<view class="">
				暂无订单
			</view>
		</view>
		<view class="noMore" v-if="noMore">
			暂无更多
		</view>
	</view>
</template>

<script>
	import {
		getOrderList,
		ordercancel,
		repay,
	} from '@/apirequest/api.js'
	export default {
		data() {
			return {
				tabArr: [{
					name: '全部',
					id: '0',
				}, {
					name: '待上传',
					id: '2',
				}, {
					name: '审核中',
					id: '3',
				}, {
					name: '已完成',
					id: '6',
				}, {
					name: '已取消',
					id: '1',
				}],
				list: [],
				current: '0',
				page: 1,
				noMore: false,
				empty: false
			}
		},
		onShow() {
			this.page = 1
			this.list = []
			this.loadDataList();
		},
		/**
		 * 页面上拉触底事件的处理函数
		 */
		onReachBottom: function() {
			if (!this.noMore) {
				this.page += 1
				this.loadDataList();
			}
		},
		methods: {
			cancelOrder(item) {
				const that = this;
				uni.showModal({
					title: '提示',
					content: item.type == 'sign' ? '确定取消报名吗?' : '确定取消订单吗?',
					success(ret) {
						if (ret.confirm) {
							ordercancel({
								id: item.id
							}).then((res) => {
								if (res.code == 1) {
									uni.showToast({
										title: res.msg,
										icon: 'success'
									});
									setTimeout(function() {
										that.page = 1
										that.list = []
										that.loadDataList();
									}, 1000);

								} else {
									uni.showToast({
										title: res.msg,
										icon: 'error'
									})
								}
							})
						}
						if (ret.cancel) {

						}
					}
				})
			},
			storeTap(item) {
				
				if (!item) {
					return;
				}
				if (parseInt(item.open_buy) == 0) {
					let time = item.open_start + "-" + item.open_end;
					uni.showToast({
						icon: 'none',
						title: '未到报名时间，报名时间' + time,
						duration: 2000
					});

				} else if (parseInt(item.stock) <= 0) {
					uni.showToast({
						icon: 'none',
						title: '今日名额已抢光',
						duration: 2000
					})
				} else {
					this.distance = item.distance;
					uni.navigateTo({
						url: `/pages/details/newDetail?id=${item.store_id}&activity_id=${item.id}&distance=${item.distance}`
					})
				}

			},
			//获取数据列表
			loadDataList() {
				const data = {
					page: this.page,
					current: this.current
				}
				this.noMore = false
				this.empty = false
				getOrderList(data).then((res) => {
					if (res.code == 1) {
						this.list = this.list.concat(...res.data.data)

						if (this.page == 1 && res.data.data.length == 0) {
							this.empty = true
						} else if (res.data.data.length < 10) {
							this.noMore = true
						}
					} else {
						uni.showToast({
							title: '网络繁忙',
							icon: 'error'
						})
					}
					this.lock = 0;
				});
			},
			goNav(item) {
				if (item.status_text == '待上传') {
					uni.navigateTo({
						url: `/pages/uploadVoucher/index?id=${item.id}`
					})
				}
				if (item.status_text == '未通过') {
					uni.navigateTo({
						url: `/pages/uploadVoucher/index?id=${item.id}`
					})
				}
			},
			upload(id) {
				uni.navigateTo({
					url: '/pages/uploadVoucher/index?id=' + id
				})
			},
			gotoWaiMai: function(platform, name) {
				if (platform == 'meituan') {
					if (name) {
						window.location.href = name;
					} else {
						uni.showToast({
							title: '暂无链接地址',
							icon: "error"
						});
					}
				} else { //饿了么复制店名
					uni.setClipboardData({
						data: name,
						success() {
							uni.showToast({
								title: '复制成功',
								icon: "success"
							});
						}
					});
				}
			},

			//订单支付
			repay(id) {
				var that = this;
				if (that.lock == 1) {
					return;
				}
				that.lock = 1;
				repay({
					id: id
				}).then((res) => {
					if (res.code == 1) {
						that.payParam = res.data.wx;
					} else {
						uni.showToast({
							title: res.msg,
							icon: "error"
						});
						that.lock = 0;
					}
				}).catch(err => {
					console.log(err)
				});

			},
			//取消订单
			cancel(id, type) {
				var that = this;
				uni.showModal({
					title: '提示',
					content: type == 'sign' ? '确定取消报名吗?' : '确定取消订单吗?',
					success(ret) {
						if (ret.confirm) {
							ordercancel({
								id: id
							}).then((res) => {
								if (res.code == 1) {
									uni.showToast({
										title: res.msg,
										icon: 'success'
									});
									setTimeout(function() {
										that.data = {
											data_list: [],
											no_list: !1,
											no_more: !1,
											current_page: 0,
											loading_more: !0,
										}
										that.loadDataList();
									}, 1000);

								} else {
									uni.showToast({
										title: res.msg,
										icon: 'error'
									})
								}
							})
						}
						if (ret.cancel) {

						}
					}
				})

			},
			tabChange(index) {
				this.current = this.tabArr[index].id
				this.noMore = false
				this.empty = false
				this.page = 1
				this.list = []
				this.loadDataList();
			},

		}
	}
</script>

<style scoped lang="scss">
	.orders {
		background-color: #f4f4f4;
		min-height: 100vh;

		::v-deep .nav-bar {
			.nav-item {
				color: #333;

				&::after {
					height: 8rpx;
					border-radius: 4rpx;
					background-color: #fe8025;
					border-bottom: none;
				}
			}

			.current::after {
				width: 50rpx;
			}
		}

		.list {
			padding: 100rpx 0;

			.li {
				width: 710rpx;
				padding: 0 20rpx;
				margin: 20rpx auto;
				background: #FFFFFF;
				border-radius: 20rpx;
				box-sizing: border-box;

				.code {
					font-size: 24rpx;
					font-family: PingFang SC;
					font-weight: 500;
					color: #333333;
					line-height: 48rpx;
					display: flex;
					align-items: center;
					justify-content: space-between;
					padding: 20rpx 0;
					box-sizing: border-box;
					border-bottom: 1rpx solid #f4f4f4;

					.status {
						color: #B30000;
					}
				}

				.content {
					display: flex;
					padding: 20rpx 0;
					box-sizing: border-box;

					.avatar {
						width: 189rpx;
						height: 189rpx;
						margin-right: 31rpx;
						border-radius: 20rpx;
					}

					.info {
						.title {
							height: 29rpx;
							font-size: 30rpx;
							font-family: PingFang SC;
							font-weight: bold;
							color: #333333;
							line-height: 48rpx;
						}

						.from {
							height: 23rpx;
							font-size: 24rpx;
							font-family: PingFang SC;
							font-weight: 500;
							color: #333333;
							line-height: 48rpx;
							opacity: 0.75;
							margin-top: 25rpx;
						}

						.jointime {
							height: 23rpx;
							font-size: 24rpx;
							font-family: PingFang SC;
							font-weight: 500;
							color: #333333;
							line-height: 48rpx;
							opacity: 0.75;
							margin-top: 34rpx;
						}

						.endtime {
							height: 23rpx;
							font-size: 24rpx;
							font-family: PingFang SC;
							font-weight: 500;
							color: #333333;
							line-height: 48rpx;
							opacity: 0.75;
							margin-top: 13rpx;
						}
					}
				}

				.foot {
					.comment {
						height: 54rpx;
						font-size: 24rpx;
						font-family: PingFang SC;
						color: #FE8429;
						background: #FFF4EC;
						border-radius: 10rpx;
						padding: 15rpx 20rpx;
						box-sizing: border-box;

					}

					.btns {
						font-size: 24rpx;
						font-family: PingFang SC;
						font-weight: bold;
						color: #FFFFFF;
						line-height: 60rpx;
						text-align: center;
						display: flex;
						align-items: center;
						justify-content: flex-end;
						margin-top: 20rpx;
						padding-bottom: 20rpx;

						.btn-1 {
							width: 158rpx;
							height: 60rpx;
							border: 1px solid #A0A0A0;
							border-radius: 30rpx;
							color: #A0A0A0;
							margin-left: 20rpx;
						}

						.btn-2 {
							width: 208rpx;
							height: 60rpx;
							background: #FE8429;
							border-radius: 30rpx;
							margin-left: 20rpx;
						}

						.btn-3 {
							width: 158rpx;
							height: 60rpx;
							background: #FFBA15;
							border-radius: 30rpx;
							margin-left: 20rpx;
						}
					}
				}
			}
		}

		.noMore {
			font-size: 24rpx;
			font-family: PingFang SC;
			color: #333333;
			text-align: center;
			padding: 0 0 40rpx 0;
		}

		.empty {
			font-size: 24rpx;
			font-family: PingFang SC;
			color: #333333;
			display: flex;
			flex-direction: column;
			align-items: center;
			justify-content: center;
			margin-top: 100rpx;

			image {
				width: 255rpx;
				height: 229rpx;
				margin-bottom: 40rpx;
			}
		}
	}
</style>